$(document).ready(function () {
    query();

});


/*查询全部公告*/
var query = function () {
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.list",
        "sAjaxSource": "http://localhost:8080/punish/queryAllPunish",
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>"
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                "sDefaultContent": "<a href='#'><span  class='label label-danger mr10 mb10' id='delete'>删除</span></a>" +
                "<a href='#'><span  class='label label-yellow mr10 mb10' id='update'>修改</span></a>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "punishId"},
            {"mDataProp": "stuInfoId"},
            {"mDataProp": "stuNum"},
            {"mDataProp": "stuName"},
            {"mDataProp": "stuSex",
                "mRender": function(data) {
                    var value = "男";
                    if (data==1){
                        value = "女";
                    }
                    return value;
                }
            },
            {"mDataProp": "academyName"},
            {"mDataProp": "punishTime"},
            {"mDataProp": "punishType"},
            {"mDataProp": "status",
                "mRender": function(data) {
                    var value = "无申请信息";
                    if (data==0){
                        value = "<span  class='label label-danger mr10 mb10'>已拒绝</span>";
                    }
                    if (data==1){
                        value = "<span  class='label label-success mr10 mb10'>处分已撤销</span>";
                    }
                    if (data==2){
                        value = "<a href='#'><span  class='label label-yellow mr10 mb10' id='agree'>同意撤销</span></a>"+
                        "<a href='#'><span  class='label label-primary mr10 mb10' id='refuse'>拒绝撤销</span></a>";
                    }
                    if (data==3){
                        value = "无申请信息";
                    }
                    return value;
                }
            },
            {"mDataProp": ""}
        ],
    })
};


$(document).on('click', '#delete', function () {
    var punishId = $(this).parents("tr").children(0).eq(0).text();
    layer.confirm('确定删除？',{
        btn:['确认','取消']
    },function () {
        $.ajax({
            url: "http://localhost:8080/punish/deletePunish",
            type: "GET",
            async: true,
            data: "punishId=" + punishId,
            dataType: "json",
            success: function () {
                location.reload();
                layer.msg("操作成功!",{icon:1});
            },
            error: function () {
                layer.alert("操作失败!");
            }
        });
    })
});


$(document).on('click', '#update', function () {
    //获取punishId
    var punishId = $(this).parents("tr").children(0).eq(0).text();
    var stuInfoId = $(this).parents("tr").children(0).eq(1).text();
    var stuNum = $(this).parents("tr").children(0).eq(2).text();
    var stuName = $(this).parents("tr").children(0).eq(3).text();
    var stuSex = $(this).parents("tr").children(0).eq(4).text();
    var academyName = $(this).parents("tr").children(0).eq(5).text();
    var punishTime = $(this).parents("tr").children(0).eq(6).text();
    var punishType = $(this).parents("tr").children(0).eq(7).text();

    //页面层
    layer.open({
        type: 2,
        title: '更新处分',
        area: ['1400px', '650px'],
        content: ['punishEdit.html?punishId='+punishId+'&stuInfoId='+stuInfoId+'&stuNum='+stuNum+'&stuName='+stuName+'&stuSex='+stuSex
                    +'&academyName='+academyName+'&punishTime='+punishTime+'&punishType='+punishType, 'no']
    })
});

$(document).on('click', '#agree', function () {
    //获取punishId
    var punishId = $(this).parents("tr").children(0).eq(0).text();
    $.ajax({
        url: "http://localhost:8080/punish/updatePunishStatus",
        type: "GET",
        async: true,
        data: "punishId=" + punishId+"&status="+1,
        dataType: "json",
        success: function () {
            location.reload();
            layer.msg("操作成功!",{icon:1});
        },
        error: function () {
            layer.alert("操作失败!");
        }
    });


});

$(document).on('click', '#refuse', function () {
    //获取punishId
    var punishId = $(this).parents("tr").children(0).eq(0).text();
    $.ajax({
        url: "http://localhost:8080/punish/updatePunishStatus",
        type: "GET",
        async: true,
        data: "punishId=" + punishId+"&status="+0,
        dataType: "json",
        success: function () {
            location.reload();
            layer.msg("操作成功!",{icon:1});
        },
        error: function () {
            layer.alert("操作失败!");
        }
    });

});



$(document).on('click', '#addNew', function () {
    //页面层
    layer.open({
        type: 2,
        title: '新增处分',
        area: ['1400px', '400px'],
        content: ['punishIssue.html', 'no']
    })

});